package com.pinoc.demo.common.es;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author yinpeng
 */
public class EsRestHighLevelClient {

	private RestHighLevelClient client;

	public static final long keepAlive = 15 * 60 * 1000;

	public RestHighLevelClient getClient() {
		return client;
	}

	public EsRestHighLevelClient() {
	}

	public EsRestHighLevelClient(String hostName, int port, String schema) {
		this.client = new RestHighLevelClient(RestClient.builder(new HttpHost(hostName, port, schema))
				.setHttpClientConfigCallback(requestConfig -> requestConfig.setKeepAliveStrategy(
						(response, context) -> keepAlive))
		);
	}

	public void close() {
		try {
			client.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
